﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.Services;
using AgileTimeTracker.Domain.Exceptions;
using AgileTimeTracker.Domain.UserModule;

namespace AgileTimeTracker.Client.Wpf.AccessControlModule
{
    public class LoginViewModel : PageViewModel
    {
        private string login;

        public Func<string> GetPassword;

        public LoginViewModel()
        {
            this.Title = "Login";
        }

        public User SelectedUser { get; set; }        

        public string Login
        {
            get { return this.login; }
            set { this.OnPropertyChanged(ref this.login, value); }
        }

        #region Commands

        public RelayCommand LoginCommand { get; set; }

        public RelayCommand SignUpCommand { get; set; }

        #endregion // Commands

        #region Initialize

        protected override void InititalizeCommands()
        {
            base.InititalizeCommands();
            this.LoginCommand = new RelayCommand(this.ExecuteLogin);
            this.SignUpCommand = new RelayCommand(this.SignUp);
        }

        #endregion // Initialize

        private void SignUp()
        {
            MainWindowViewModel.Instance.CurrentView = new SignUpViewModel();
        }

        private void ExecuteLogin()
        {
            var user = AppService.Instance.Login(this.login, this.GetPassword());

            if(user == null)
            {
                throw new DomainException("Usuário não cadastrado ou senha inválida.");
            }

            MainWindowViewModel.Instance.CurrentUser = user;
            MainWindowViewModel.Instance.CurrentView = new ProjectSelectionViewModel();
        }
    }
}
